﻿<!DOCTYPE html>
<html>
<head>
<!--
  Copyright (c) 2015 Jean-Marc VIGLINO, 
  released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
-->
  <title>ol-ext: layer preview</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <!-- jQuery -->
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>

  <!-- Openlayers -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@latest/ol.css" />
  <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/ol@latest/dist/ol.js"></script>

  
  <!-- ol-ext -->
  <link rel="stylesheet" href="../../dist/ol-ext.css" />
  <script type="text/javascript" src="../../dist/ol-ext.js"></script>
  <!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
  <script src="https://unpkg.com/elm-pep"></script>

  <link rel="stylesheet" href="../style.css" />
  <style>
    #preview .img {
      display:block;
      width:128px;
      height:92px;
      position:relative;
      overflow:hidden;
      margin-top:1em;
      box-shadow: 2px 2px 2px 2px rgba(0, 0, 0, 0.5)
    }
    #preview img {
      position:absolute;
      bottom:0;
      max-width:128px;
    }
  </style>
</head>
<body >
  <a href="https://github.com/Viglino/ol-ext" class="icss-github-corner"><i></i></a>

  <a href="../../index.html">
    <h1>ol-ext: layer preview</h1>
  </a>
  <div class="info">
    Get a preview image for a layer.
    <br />
    It will add a <i>getPreview(lonlat, res)</i> function to layers to get an image preview of it.
    <ul>
    <li>
    The function return an array of previews (group layers will return a preview for each layers in it).</li>
    <li>
    You can add a preview image on layer that has no preview (vector layers for examples).
    </li>
    <li>
    See how it runs on <a href="../control/map.switcher.image.html">Layer switcher image example</a>.
    </li>
    </ul>
  </div>

  <!-- map div -->
  <div id="map" style="width:600px; height:400px;"></div>

  <div id="preview" class="options" style="padding:1em;">
    Preview: <select></select>
    <div class="img"></div>
  </div>
  
  <script type="text/javascript">

  // Two base layers
  var carto = new ol.layer.Geoportail({ layer: 'GEOGRAPHICALGRIDSYSTEMS.PLANIGNV2' });
  var osm = new ol.layer.Tile({
    title: "OSM",
    baseLayer: true,
    source: new ol.source.OSM(),
    visible: false
  });

  // WMS Layer BRGM
  var brgm = new ol.layer.Tile({
    name: "Geologie (WMS)",
    visible: false,
    minResolution: 200000 /(72/2.54*100),
    maxResolution: 10000000 /(72/2.54*100),
    source: new ol.source.TileWMS({
      url: 'https://geoservices.brgm.fr/geologie',
      attributions: [ '&copy; BRGM (French USGS equivalent)' ],
      projection:"EPSG:3857",
      params: {
        'LAYERS': 'SCAN_F_GEOL1M', //'SCAN_D_GEOL50', 
        'FORMAT': 'image/png',
        'VERSION': '1.1.1'
      }
      //crossOrigin: 'anonymous',
    })
  });
        // GeoJSON layer
  var vectorSource = new ol.source.Vector({
    url: '../data/fond_guerre.geojson',
    projection: 'EPSG:3857',
    format: new ol.format.GeoJSON(),
    attributions: [ "&copy; <a href='https://data.culture.gouv.fr/explore/dataset/fonds-de-la-guerre-14-18-extrait-de-la-base-memoire'>data.culture.gouv.fr</a>" ],
    logo:"https://www.data.gouv.fr/s/avatars/37/e56718abd4465985ddde68b33be1ef.jpg" 
  });
  var vector = new ol.layer.Vector({
    name: '1914-18 (vector)',
    // Set the preview for  vector layer
    preview: "http://www.culture.gouv.fr/Wave/image/memoire/2484/sap40_z0002136_v.jpg",
    source: vectorSource
  });

  // The map
  var map = new ol.Map({
    target: 'map',
    //renderer: ["canvas", "webgl", "dom"],  /* ["webgl", "canvas", "dom"] */
    view: new ol.View({
      zoom: 6,
      center: [270148, 6247782]
    }),
    layers: [osm,carto,brgm, vector]
  });
  // Add a new Layerswitcher to the map
  map.addControl(new ol.control.LayerSwitcher());

  // Fill select with maps layer
  var select = $("#preview select").on("change", function () { showPreview(layers[this.value]);} );
  var layers = map.getLayers().getArray();
  for (var i = 0; i<layers.length; i++) { 
    $("<option>").val(i)
      .text(layers[i].get('title') || layers[i].get('name'))
      .appendTo(select);
  }
  // Display preview in the img div
  function showPreview (l) {
    $("#preview .img").html("");
    var pv = l.getPreview(map.getView().getCenter());
    for (var i=0; i<pv.length; i++) $("<img>").attr('src',pv[i]).appendTo("#preview .img");
  }
  showPreview(layers[0]);

  </script>
  
</body>
</html>